﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
    }).use(['element', 'jquery', 'layer', 'table', 'form', 'common'], function () {
    var $ = layui.$,
        form = layui.form,
        util = layui.util,
        table = layui.table,
        common = layui.common,
        layer = layui.layer;
    //必须在框架内使用
    common.noneIframe();
    var tableCategory = table.render({
        elem: '#tableCategory',
        url: '/Ashx/article.ashx', //数据接口
        method: 'post',
        toolbar: "#toolbarBtn",
        defaultToolbar: [],
        where: { functionname: 'jhy_category_query'},
        cols: [[ //表头   

            { field: 'cate_name', title: '类别名称', event: 'cate_name' },           
            { title: "操作", templet: "#toolbarDone", width: 80, align: "center" }
        ]]
    });


    //添加类别
    table.on('toolbar(tableCategory)', function (obj) {       
        if (obj.event == "add") {
            layer.prompt({
                formType: 0,               
                title: '请输类别名称'
            }, function (value, index, elem) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/article.ashx",
                    data: { functionname: "jhy_category_add", cate_name: value },
                    success: function (msg) {
                        if (msg == 'true') {
                            layer.msg("成功");                            
                        } else {
                            layer.msg("失败");                            
                        }
                        tableCategory.reload();
                        layer.close(index);
                    }
                });                
            });
        } 
    });

    //修改类别名称、删除类别
    table.on('tool(tableCategory)', function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'cate_name') { //查看
            layer.prompt({
                formType: 0,
                title: '修改类别名称',
                value: data.cate_name
            }, function (value, index, elem) {
                if (value != data.cate_name) {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/article.ashx",
                        data: { functionname: "jhy_category_edit", cate_id: data.cate_id, cate_name: value },
                        success: function (msg) {
                            if (msg == 'true') {
                                layer.msg("成功");
                                obj.update({
                                    cate_name: value
                                });
                            } else {
                                layer.msg("失败");
                            }
                           
                            layer.close(index);
                        }
                    });
                } else
                {
                    layer.close(index);
                }
               
            });
        } else if (layEvent === 'delete') { //删除
            layer.confirm('真的删除' + data.cate_name + '?', { icon: 3, title: "删除提示" }, function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/article.ashx",
                    data: { functionname: "jhy_category_delete", cate_id: data.cate_id},
                    success: function (msg) {
                        if (msg == 'true') {
                            layer.msg("成功");
                            obj.del(); 
                        } else {
                            layer.msg("失败");
                            tableCategory.reload();
                        }
                        layer.close(index);
                    }
                });
            });
        } 
    });
  
});  
